Let’s define some vectors which can be used for demonstrations:
manyNumbers <- sample( 1:1000, 20 )
manyNumbers
[1] 288 497 429 1 772 685 39 42 890 525 307 985 67 821 132 141 78 860 905 383
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
[1] 860 NA 42 429 497 890 383 132 772 39 685 141 288 905 1 NA 985 67 307 821 NA 78 525
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
[1] 3 1 2 5 4 4 5 4 3 1
letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
[1] "q" "i" "r" "a" "o" "R" "U" "Q" "J" "K"
manyNumbersWithNA instead of manyNumbers.all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE
Input: logical vector Output: vector of numbers (positions)
which( manyNumbers > 900 )
[1] 12 19
which( manyNumbersWithNA > 900 )
[1] 14 17
which( is.na( manyNumbersWithNA ) )
[1] 2 16 21
manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 985 905
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 985 905
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 985 905
"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "R" "U" "Q" "J" "K"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "q" "i" "r" "a" "o"
manyNumbers %in% 300:600
[1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
which( manyNumbers %in% 300:600 )
[1] 2 3 10 11 20
sum( manyNumbers %in% 300:600 )
[1] 5
NAsif_else( manyNumbersWithNA >= 500, "large", "small" )
[1] "large" NA "small" "small" "small" "large" "small" "small" "large" "small" "large" "small" "small" "large" "small" NA "large" "small" "small" "large"
[21] NA "small" "large"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
[1] "large" "UNKNOWN" "small" "small" "small" "large" "small" "small" "large" "small" "large" "small" "small" "large" "small" "UNKNOWN"
[17] "large" "small" "small" "large" "UNKNOWN" "small" "large"
# here integer 0L is needed instead of real 0.0
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L )
[1] 860 NA 0 0 0 890 0 0 772 0 685 0 0 905 0 NA 985 0 0 821 NA 0 525
unique( duplicatedNumbers )
[1] 3 1 2 5 4
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA 3 1 2 5 4
duplicated( duplicatedNumbers )
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
which.max( manyNumbersWithNA )
[1] 17
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 985
which.min( manyNumbersWithNA )
[1] 15
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 1
range( manyNumbersWithNA, na.rm = TRUE )
[1] 1 985
manyNumbersWithNA
[1] 860 NA 42 429 497 890 383 132 772 39 685 141 288 905 1 NA 985 67 307 821 NA 78 525
sort( manyNumbersWithNA )
[1] 1 39 42 67 78 132 141 288 307 383 429 497 525 685 772 821 860 890 905 985
sort( manyNumbersWithNA, na.last = TRUE )
[1] 1 39 42 67 78 132 141 288 307 383 429 497 525 685 772 821 860 890 905 985 NA NA NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
[1] 985 905 890 860 821 772 685 525 497 429 383 307 288 141 132 78 67 42 39 1 NA NA NA
manyNumbersWithNA[1:5]
[1] 860 NA 42 429 497
order( manyNumbersWithNA[1:5] )
[1] 3 4 5 1 2
rank( manyNumbersWithNA[1:5] )
[1] 4 5 1 2 3
sort( mixedLetters )
[1] "a" "i" "J" "K" "o" "q" "Q" "r" "R" "U"
manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
[1] 1.0 6.5 6.5 3.5 9.5 6.5 6.5 3.5 9.5 2.0
rank( manyDuplicates, ties.method = "min" )
[1] 1 5 5 3 9 5 5 3 9 2
rank( manyDuplicates, ties.method = "random" )
[1] 1 5 7 3 9 6 8 4 10 2
v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
[1] -1.00000000 -0.50000000 0.00000000 0.50000000 1.00000000 0.44541061 0.23921943 -0.75785854 -0.18035480 1.92545618 -0.97256521 1.49652988 -0.39701142
[14] 0.08385421 0.72845633
round( v, 0 )
[1] -1 0 0 0 1 0 0 -1 0 2 -1 1 0 0 1
round( v, 1 )
[1] -1.0 -0.5 0.0 0.5 1.0 0.4 0.2 -0.8 -0.2 1.9 -1.0 1.5 -0.4 0.1 0.7
round( v, 2 )
[1] -1.00 -0.50 0.00 0.50 1.00 0.45 0.24 -0.76 -0.18 1.93 -0.97 1.50 -0.40 0.08 0.73
floor( v )
[1] -1 -1 0 0 1 0 0 -1 -1 1 -1 1 -1 0 0
ceiling( v )
[1] -1 0 0 1 1 1 1 0 0 2 0 2 0 1 1
heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob
166 170 177
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB
166 170 177
heights[[ "EVE" ]]
[1] 170
expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 × 2
x y
<int> <chr>
1 1 a
2 1 b
3 2 a
4 2 b
5 3 a
6 3 b
7 NA a
8 NA b
combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
[2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e"
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
[2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
[3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC